//
//  ViewController.swift
//  ZHM
//
//  Created by zzddongge on 16/7/18.
//  Copyright © 2016年 zzd. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    
    //获取屏幕的宽度和高度
    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    var scrollView:UIScrollView!
    let numOfPages = 3
    //创建开始体验按钮
    var startBtn:UIButton?
    //创建UIPageControl对象
    var pageControl:UIPageControl?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let frame = self.view.bounds
        
        
        //创建开始体验按钮
        startBtn = UIButton(frame:CGRect(x: 100,y: screenHeight-120,width: screenWidth-200,height: 30))
        startBtn!.setTitle("开始体验", for: UIControlState())
        startBtn!.layer.cornerRadius = 15
        startBtn!.backgroundColor = UIColor(red: 92/255,green: 172/255,blue:238/255,alpha: 1.0)
        
        pageControl = UIPageControl(frame:CGRect(x: 150,y: screenHeight-70,width: screenWidth-300,height: 30))
        
        //设置scrollView的相关属性
        scrollView = UIScrollView(frame:frame)
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        scrollView.contentOffset = CGPoint.zero
        
        //将scrollView的contentSize设为屏幕宽度的3倍
        scrollView.contentSize = CGSize(width: screenWidth * CGFloat(numOfPages),height:screenHeight)
        scrollView.delegate = self
        
        for index  in 0..<numOfPages {
            let imageView = UIImageView(image: UIImage(named: "ZHHZJXa.0\(index + 1).L"))
            imageView.frame = CGRect(x: frame.size.width * CGFloat(index), y: 0, width: frame.size.width, height: frame.size.height)
            scrollView.addSubview(imageView)
        }

        //设置pageCtroll的相关属性
        pageControl!.numberOfPages = numOfPages
        pageControl!.currentPage = 0
        self.view.insertSubview(scrollView, at: 0)
        self.view.addSubview(startBtn!)
        self.view.addSubview(pageControl!)
        self.navigationController?.isNavigationBarHidden = true
        // 隐藏开始按钮
        startBtn!.alpha = 0.0
        startBtn!.addTarget(self, action: #selector(startExperience), for: .touchUpInside)
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    // 隐藏状态栏
    override var prefersStatusBarHidden : Bool {
        return true
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func startExperience(){
        let mainStoryboard = UIStoryboard(name: "Main",bundle: Bundle.main)
        let shouYeViewController = mainStoryboard.instantiateViewController(withIdentifier: "shouYeViewController") as! ShouYeViewController
        let navigationViewController = UINavigationController.init(rootViewController: shouYeViewController)
        self.present(navigationViewController, animated: true, completion: nil)
    }
}

// MARK: - UIScrollViewDelegate
extension ViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        // 随着滑动改变pageControl的状态
        pageControl!.currentPage = Int(offset.x / view.bounds.width)
        
        // 因为currentPage是从0开始，所以numOfPages减1
        if pageControl!.currentPage == numOfPages - 1 {
            UIView.animate(withDuration: 0.5, animations: {
                self.startBtn!.alpha = 1.0
            }) 
        } else {
            UIView.animate(withDuration: 0.2, animations: {
                self.startBtn!.alpha = 0.0
            }) 
        }
    }
}
